package com.example.back.service.impl;

import cn.hutool.core.util.IdUtil;
import com.example.back.dto.req.GetEscortProductList;
import com.example.back.dto.resp.EscortProductList;
import com.example.back.mapper.EscortProductMapper;
import com.example.back.service.EscortProductService;
import com.example.common.entity.EscortProduct;
import com.example.common.support.ApiException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @ClassName EscortProductServiceImpl
 * @Author wengjt
 * @Date 2021/5/7 14:01
 * @Description
 **/
@Service
public class EscortProductServiceImpl implements EscortProductService {
    @Resource
    EscortProductMapper escortProductMapper;

    /**
     * 多条件查询陪护套餐列表
     *
     * @Author wengjt
     * @Date 14:24 2021/5/7
     */
    @Override
    public EscortProductList getEscortProductList(GetEscortProductList dto) {
        EscortProductList result = new EscortProductList();
        PageInfo<EscortProduct> pageInfo = PageHelper.startPage(dto.getPageNum(), dto.getPageSize()).doSelectPageInfo(() -> {
            escortProductMapper.multipleSearch(dto.getEscortProductName(), dto.getCtId(), dto.getShow());
        });
        result.setCount(pageInfo.getTotal());
        result.setList(pageInfo.getList());
        return result;
    }

    /**
     * 根据id查询单个陪护套餐详细
     *
     * @Author wengjt
     * @Date 14:24 2021/5/7
     */
    @Override
    public EscortProduct getEscortProduct(Long cpId) {
        return escortProductMapper.findById(cpId);
    }

    /**
     * 修改单个陪护套餐
     *
     * @Author wengjt
     * @Date 14:31 2021/5/7
     */
    @Override
    public Object updateEscortProduct(EscortProduct escortProduct) {
        EscortProduct one = escortProductMapper.findById(escortProduct.getCpId());

        if (one.getDisable() == 1) {
            throw new ApiException(400, "不允许修改已关闭套餐");
        }
        if (one.getShowAble() == 1) {
            throw new ApiException(400, "不允许修改已经上架套餐");
        }
        one.setName(escortProduct.getName());
        one.setCtId(escortProduct.getCtId());
        one.setServiceObject(escortProduct.getServiceObject());
        one.setServiceContent(escortProduct.getServiceContent());
        one.setNotice(escortProduct.getNotice());
        one.setPrice(escortProduct.getPrice());
        one.setSpec(escortProduct.getSpec());
        one.setLimitTime(escortProduct.getLimitTime());
        one.setShowAble(escortProduct.getShowAble());
        one.setIcon(escortProduct.getIcon());
        escortProductMapper.updateById(one);
        return null;
    }

    /**
     * 新增单个套餐
     *
     * @Author wengjt
     * @Date 14:57 2021/5/7
     */
    @Override
    public Object insertEscortProduct(EscortProduct escortProduct) {
        escortProduct.setDisable(0);
        escortProduct.setCreateTime(new Date());
        escortProductMapper.insertOne(escortProduct);
        return null;
    }

    /**
     * 删除单个套餐
     *
     * @Author wengjt
     * @Date 15:02 2021/5/7
     */
    @Override
    public Object deleteEscortProduct(Long cpId) {
        EscortProduct escortProduct = escortProductMapper.findById(cpId);
        if (escortProduct.getDisable() == 1) {
            throw new ApiException(400, "不允许删除已关闭套餐");
        }
        if (escortProduct.getShowAble() == 1) {
            throw new ApiException(400, "不允许删除已经上架套餐");
        }
        escortProductMapper.deleteById(cpId);
        return null;
    }

    /**
     * 套餐上下架
     *
     * @Author wengjt
     * @Date 15:13 2021/5/7
     */
    @Override
    public Object updateShowAbleById(Long cpId, Integer showAble) {
        EscortProduct escortProduct = escortProductMapper.findById(cpId);
        if (escortProduct.getDisable() == 1) {
            throw new ApiException(400, "不允许修改已关闭套餐");
        }
        escortProduct.setShowAble(showAble);
        escortProductMapper.updateById(escortProduct);
        return null;
    }

}
